package com.topone.service.impl;

import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.dto.OrderDTO;
import com.topone.service.BuyerService;
import com.topone.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/12/5,0005.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private OrderService orderService;
    @Override
    public OrderDTO findOrderOne(String orderId,String openid) {
        return checkOrderOwner(orderId,openid);
    }

    @Override
    public OrderDTO cancelOrderOne(String orderId,String openid) {
        OrderDTO orderDTO=checkOrderOwner(orderId,openid);
        if(orderDTO==null){
            log.error("【订单取消】差不到该订单 orderDt={}",orderDTO);
            throw  new FanZoneException(ResultEnum.ORDER_NOT_EXIST);
        }
        return orderDTO;
    }

    public OrderDTO checkOrderOwner(String orderId,String openid){
        OrderDTO orderDTO=orderService.findOne(orderId);
        if(orderDTO==null){
            log.error("【订单取消】差不到该订单 orderDt={}",orderDTO);
            throw  new FanZoneException(ResultEnum.ORDER_NOT_EXIST);
        }
        if(!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("【订单查询】 订单的openid不一致，openid={},orderDto={}",openid,orderDTO);
            throw new FanZoneException(ResultEnum.ORDER_OWNER_ERROR);
        }
          return orderDTO;
    }
}
